﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyBanHang.OBJECT;
using System.Data.SqlClient;
using System.Data;
using MyDataService;
using QuanLyBanHang.Report;
namespace QuanLyBanHang.DAL
{
    public class DalBanHang
    {

        public  void loadcboHangHoa(ComboBox cbo)
        {
            cbo.DataSource = dataservice.selectDatatable("select MaHang,TenHang from HangHoa Where TrangThai=1");
            cbo.DisplayMember = "TenHang";
            cbo.ValueMember = "MaHang";
        }

        public bool BanHangHoa(DataGridView dataGridView)
        {
            bool Result = false;
            string MaHDB = CreateKeyHoadon();
            // Tạo hóa đơn
            SqlParameter praHoaDon=new SqlParameter("MaHdB",MaHDB);
            string sqlinsertHoadon = "Insert into HoaDonBan values(@MaHdB,'" + SessionPrivate.NhanVien.MaNV +
                                     "',GETDATE())";
            dataservice.runQuery(sqlinsertHoadon,praHoaDon);
            

            for (int i = 0; i < dataGridView.Rows.Count; i++)
            {
                string MaHang = dataGridView.Rows[i].Cells[0].Value.ToString();
                int SoLuong = int.Parse(dataGridView.Rows[i].Cells[3].Value.ToString());
                 int slCu = exMaVach(MaHang);
                 slCu -= SoLuong;
                 dataservice.runQuery("Insert into CT_HoaDonBan values('" + MaHDB + "','" + MaHang + "'," + SoLuong + ")");
                dataservice.runQuery("Update HangHoa set SoLuong =" + slCu + " where MaVach='" + MaHang + "'");
            }

            frmInHoaDon inHoaDon=new frmInHoaDon(MaHDB);
            inHoaDon.ShowDialog();
            return Result;
        }

      

        public string CreateKeyHoadon()
        {   string Key = "";
            string CountHoadon = dataservice.selectScalar("select Top 1 MaHDB from HoaDonBan order by MaHDB desc").ToString();
            int CountMax = int.Parse(CountHoadon.Substring(3, 7));
            CountMax += 1;
            Key = string.Format("{0:HDB0000000}", CountMax);
            return Key;
        }

        public int exMaVach(string MaVach)
        {
            var SoLuong =dataservice.selectScalar("select SoLuong from HangHoa Where MaVach='" + MaVach + "'");
            if (SoLuong==null)
            {
                SoLuong = "0";
            }
            return int.Parse(SoLuong.ToString());
        }

        public ObjHangHoa genRandom()
        {
            DataTable dt = dataservice.selectDatatable("select top 1 * from HangHoa order by NEWID()");
            ObjHangHoa objHangHoa =ConverttoObj(dt.Rows[0]);
            return objHangHoa;

        }

        public ObjHangHoa ConverttoObj(DataRow dataRow)
        {
            ObjHangHoa objHangHoa=new ObjHangHoa();
            objHangHoa.Ma = dataRow["MaHang"].ToString();
            objHangHoa.Ten = dataRow["TenHang"].ToString();
            objHangHoa.Giaban = Convert.ToDouble(dataRow["GiaBan"].ToString());
            return objHangHoa;
        }
        public bool exHanSuDung(string MaHang)
        {
            bool Result = false;
            var Han = dataservice.selectScalar("select cast((HSD-GETDATE()) as int) from HangHoa where MaHang='" + MaHang + "'");
            if (Convert.ToInt32(Han)>0)
            {
                Result = true;
            }
            return Result;
        }

        public bool DanhDau(string MaHang,string NoiDung)
        {
            return
                dataservice.runQuery("Insert into DanhDauHangHoa Values(N'" + MaHang + "',N'" + NoiDung + "',GETDATE())");
        }
    }
}
